package com.swak.reactivex.transport;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.InetSocketAddress;

import com.swak.exception.RemotingException;
import com.swak.registry.URL;

/**
 * 连接
 * 
 * @author lifeng
 */
public interface Channel {

	/**
	 * Channel 的配置信息
	 * 
	 * @return
	 */
	URL url();

	/**
	 * 连接状态
	 * 
	 * @return
	 */
	boolean isConnected();

	/**
	 * 不需要等待发送结束
	 * 
	 * @param message
	 * @throws RemotingException
	 */
	default void send(Object message) throws RemotingException {
		this.send(message, false);
	}

	/**
	 * 发布消息
	 *
	 * @param message
	 * @param sent    wait sent finish
	 */
	void send(Object message, boolean sent) throws RemotingException;

	/**
	 * 发送文件
	 * 
	 * @param file 文件
	 * @param sent 是否等待发送结束
	 * @throws RemotingException
	 * @throws FileNotFoundException
	 */
	default void sendFile(File file, boolean sent) throws RemotingException, FileNotFoundException {
	}

	/**
	 * 关闭连接
	 */
	void close();

	/**
	 * 本地连接
	 * 
	 * @return
	 */
	InetSocketAddress localAddress();

	/**
	 * 远端连接
	 * 
	 * @return
	 */
	InetSocketAddress remoteAddress();

	/**
	 * has attribute.
	 *
	 * @param key key.
	 * @return has or has not.
	 */
	boolean hasAttribute(String key);

	/**
	 * get attribute.
	 *
	 * @param key key.
	 * @return value.
	 */
	Object getAttribute(String key);

	/**
	 * set attribute.
	 *
	 * @param key   key.
	 * @param value value.
	 */
	void setAttribute(String key, Object value);

	/**
	 * remove attribute.
	 *
	 * @param key key.
	 */
	void removeAttribute(String key);
}
